
package com.framework.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {

    @Bean(value = "defaultApi")
    @Order(value = 4)
    public Docket defaultApi() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters= Lists.newArrayList();

		//默认的3个参数
		parameterBuilder
				.name("token")
				.description("身份校验的token")
				.modelRef(new ModelRef("string"))
				.parameterType("query")
				.required(false).build();
		parameters.add(parameterBuilder.build());

		parameterBuilder
				.name("sign")
				.description("数据加密校验 1.AES方式加密 2.除去sign参数，其它参数名+参数 Unicode码从小到大排序（字典序)")
				.modelRef(new ModelRef("String"))
				.parameterType("query")
				.required(true).build();
		parameters.add(parameterBuilder.build());

		parameterBuilder
				.name("request_time")
				.description("当前时间戳")
				.modelRef(new ModelRef("long"))
				.parameterType("query")
				.required(true).build();
		parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("mpos-api接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.framework.controller.newcontroller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameters)
                .securityContexts(Lists.newArrayList(securityContext()));
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Mpos接口文档")
                .description("Mpos接口文档说明")
				.licenseUrl("127.0.0.1")
				.license("12")
                .contact(new Contact("程康", "http://192.168.0.140:8081/mpos-api/v1/doc.html", "874845331@qq.com"))
                .version("1.0")
                .build();
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
    }
}
